package com.sidianzhong.sdz.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileDownload {
	public  static ResponseEntity<ResultModel> checkUpdate(String path) throws IOException {
		File file = new File(path);
		if(!file.exists()){
		    //先得到文件的上级目录，并创建上级目录，在创建文件
		    file.getParentFile().mkdir();
		    try {
		        //创建文件
		        file.createNewFile();
		    } catch (IOException e) {
		        e.printStackTrace();
		    }
		}
		String charset = "UTF-8"; 
		FileInputStream inputStream = new FileInputStream(file); 
		InputStreamReader reader = new InputStreamReader(inputStream, charset); 
		StringBuffer buffer = new StringBuffer(); 
		char[] buf = new char[64]; 
		int count = 0; 
		try { 
			while ((count = reader.read(buf)) != -1) { 
				buffer.append(buf, 0, count); 
			}
		} finally { 
			reader.close(); 
		}
		JSONObject newJson = JSON.parseObject(buffer.toString());
		if(newJson==null) {
			JSONObject nullJson=new JSONObject();
			return new ResponseEntity<>(ResultModel.ok(nullJson), HttpStatus.OK);
		}else {
			//1 表示强制更新；0 表示不强制更新
			return new ResponseEntity<>(ResultModel.ok(newJson), HttpStatus.OK);
		}
	}
}
